<?php
	/*
		@copyright HungHau Holding
		@Name Le Van Tuong
		@Email levantuong435@gmail.com
	*/
	if(!function_exists('DiemKhuVuc')){
		function DiemKhuVuc($khuvuc,$uutien,$tongdiem){
			if(is_numeric($tongdiem)){
				// Cong diem tuong ung theo khu vuc
				if($uutien == 3){
					switch($khuvuc){
						case "3"			:		$diem = $tongdiem;						break;
						case "2"			:		$diem = $tongdiem + 50;		break;
						case "2NT"			:		$diem = $tongdiem + 100;	break;
						case "1"			:		$diem = $tongdiem + 150;	break;
					}
				}elseif ($uutien == 2){
					switch($khuvuc){
						case "3"			:		$diem = $tongdiem + 100;	break;
						case "2"			:		$diem = $tongdiem + 150;	break;
						case "2NT"			:		$diem = $tongdiem + 200;	break;
						case "1"			:		$diem = $tongdiem + 250;	break;
					}
				}else{
					switch($khuvuc){
						case "3"			:		$diem = $tongdiem + 200;	break;
						case "2"			:		$diem = $tongdiem + 250;	break;
						case "2NT"			:		$diem = $tongdiem + 300;	break;
						case "1"			:		$diem = $tongdiem + 350;	break;
					}
				}
			}else{
				$diem = '';
			}
			return $diem;
		}
	}// End DiemKhuVuc
	if(!function_exists('DiemTongCong')){
		// Xu Ly Diem Tong Cong NVBS
		function DiemTongCong($DM1,$DM2,$DM3){
			if(is_numeric($DM1) && is_numeric($DM2) && is_numeric($DM3)){
				$diem = ($DM1 + $DM2 + $DM3);
			}else{
				$diem = '';
			}
			if(strlen($diem) == 3){
				return '0'.$diem;
			}else{
				return $diem;
			}
		}
	}// DiemTongCongNVBS
	if(!function_exists('DiemLamTron')){
		// Xu Ly Diem Lam Trong Khong Thap Phan
		function DiemLamTron($dtc){
			// Kiểm tra dữ liệu có phải là số nguyên hay không
			if(is_numeric($dtc)){
				if(strlen($dtc) == 4){
					$thapphan = substr($dtc,2,4);
					if($thapphan == 25 || $thapphan == 75){
						$diem = $dtc + 25;
					}else{
						$diem = $dtc;
					}
				}else{
					$thapphan = substr($dtc,1,3);
					if($thapphan == 25 || $thapphan == 75){
						$diem = $dtc + 25;
					}
					else{
						$diem = $dtc;
					}
				}
			}else{
				$diem = '';
			}
			if(strlen($diem) == 3){
				return '0'.$diem;
			}else{
				return $diem;
			}
		}
	}// DiemTronNVBS
	if(!function_exists('DiemXuLy')){
		function DiemXuLy($Diem){
			$diem_tach  = str_replace('.','',$Diem);
			if(strlen($diem_tach) == 3){
				$resulf = '0'.$diem_tach;
			}else{
				$resulf = $diem_tach;
			}
			return $resulf;
		}
	}
	if(!function_exists('LuuDiemKhuVuc')){
		// Xu Ly Phan Thong Tin Luu Vao DB DVH_NVBS
		function LuuDiemKhuVuc($khuvuc,$uutien){
			if($uutien == "3"){
				switch($khuvuc){
					case "3"			:		$diem = 0;		break;
					case "2"			:		$diem = 0.5;	break;
					case "2NT"			:		$diem = 1.0;	break;
					case "1"			:		$diem = 1.5;	break;
				}
			}elseif($uutien == "2"){
				switch($khuvuc){
					case "3"			:		$diem = 1.0;	break;
					case "2"			:		$diem = 1.5;	break;
					case "2NT"			:		$diem = 2.0;	break;
					case "1"			:		$diem = 2.5;	break;
				}
			}elseif($uutien == "1"){
				switch($khuvuc){
					case "3"			:		$diem = 2.0;	break;
					case "2"			:		$diem = 2.5;	break;
					case "2NT"			:		$diem = 3.0;	break;
					case "1"			:		$diem = 3.5;	break;
				}
			}
			return $diem;
		}
	}// LuuDiemKhuVuc
	if(!function_exists("NhomUT")){
		function NhomUT($doituong){
			if($doituong == 01 || $doituong == 02 || $doituong == 03 || $doituong == 04){
				$nhomut = 1;
			}elseif($doituong == 05 || $doituong == 06 || $doituong == 07){
				$nhomut = 2;
			}else{
				$nhomut = 3;
			}
			return $nhomut;
		}
	}
/* End xử lý điểm */
	// Tiện ích
	if(!function_exists('DiemInputOld')){
		function DiemInputOld($diem){
			if(strlen($diem) == 3){
				$thapphan = substr($diem,1,3);
				$_diem = "0".substr($diem,0,1).".".$thapphan;
			}else{
				$thapphan = substr($diem,2,4);
				$_diem = substr($diem,0,2).".".$thapphan;
			}
			return $_diem;
		}
	}
	if(!function_exists('HeDaoTao')){
		function HeDaoTao($nganh){
			$_tach_chuoi = substr($nganh,0,1);
			if($_tach_chuoi == 'D'){
				$hedaotao = 1;
			}else{
				$hedaotao = 2;
			}
			return $hedaotao;
		}
	}
	if(!function_exists('convertDate')){
		function convertDate($date) {
			$date = preg_replace('/\D/','-',$date);
			return date('Y-m-d',strtotime($date));
		}
	}
	// Check đậu rớt
	if(!function_exists('CheckDauRot')){
		function CheckDauRot(array $array){
			if(is_array($array)){
				$uutien = NhomUT($array['doituong']);
				$khuvuc = $array['khuvuc'];
				$diemchuan = str_replace(".","",$array['diemchuan']);
				// Học kỳ lớp I 11
				$txtHK1_11_Mon1 = str_replace(".","", $array['txtHK1_11_Mon1']);
				$txtHK1_11_Mon2	= str_replace(".","", $array['txtHK1_11_Mon2']);
				$txtHK1_11_Mon3	= str_replace(".","", $array['txtHK1_11_Mon3']);
				// Học kỳ lớp II 11
				$txtHK2_11_Mon1 = str_replace(".","", $array['txtHK2_11_Mon1']);
				$txtHK2_11_Mon2	= str_replace(".","", $array['txtHK2_11_Mon2']);
				$txtHK2_11_Mon3	= str_replace(".","", $array['txtHK2_11_Mon3']);
				// Học kỳ lớp I 12
				$txtHK1_12_Mon1 = str_replace(".","", $array['txtHK1_12_Mon1']);
				$txtHK1_12_Mon2	= str_replace(".","", $array['txtHK1_12_Mon2']);
				$txtHK1_12_Mon3	= str_replace(".","", $array['txtHK1_12_Mon3']);
				// Xử lý điểm
				$diem_mon1 = ceil(DiemTongCong($txtHK1_11_Mon1,$txtHK2_11_Mon1,$txtHK1_12_Mon1)/3);
				$diem_mon2 = ceil(DiemTongCong($txtHK1_11_Mon2,$txtHK2_11_Mon2,$txtHK1_12_Mon2)/3);
				$diem_mon3 = ceil(DiemTongCong($txtHK1_11_Mon3,$txtHK2_11_Mon3,$txtHK1_12_Mon3)/3);
				$diem_tongcong = DiemTongCong($diem_mon1,$diem_mon2,$diem_mon3);
				// Xử lý điểm
				$diemkhuvuc = DiemKhuVuc($khuvuc,$uutien,$diem_tongcong);
				if($diemkhuvuc >= $diemchuan){
					return true;
				}else{
					return false;
				}
			}
		}
	}
?>